package com.bwei.like.mynewstop.adapater;

import android.content.Context;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.VideoView;

import com.bwei.like.mynewstop.Bean.NewsCategoryRoot;
import com.bwei.like.mynewstop.R;
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
import com.bwei.like.mynewstop.app.MyApplication;
>>>>>>> NewsTop
import com.bwei.like.mynewstop.base.MyBaseAdapater;
import com.bwei.like.mynewstop.utils.CommonUtils;
import com.bwei.like.mynewstop.utils.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by LiKe on 2016/11/3.
 */
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
public class NewsCategoryAdapater extends MyBaseAdapater<NewsCategoryRoot.DataBean> implements View.OnClickListener{
=======
public class NewsCategoryAdapater extends MyBaseAdapater<NewsCategoryRoot.DataBean> implements View.OnClickListener {
>>>>>>> NewsTop

    /**
     * 普通条目类型
     */
    public static final int TYPE_NORMAL = 0;
    /**
     * 大图片条目类型
     */
    public static final int TYPE_LARGE_PIC = 1;
    /**
     * 没有图片类型
     */
    public static final int TYPE_NO_PIC = 2;
    /**
     * 多图片类型
     */
    public static final int TYPE_MULITY_PIC = 3;
    /*
    视频类型
     */
    public static final int TYPE_VIDEO = 4;
    private final DisplayImageOptions initOptions;
    private LinearLayout popup_linearLayout;


    public NewsCategoryAdapater(ArrayList<NewsCategoryRoot.DataBean> arrayList, Context context) {
        super(arrayList, context);
        initOptions = ImageLoaderUtils.initOptions();
    }


    @Override
    public int getItemViewType(int position) {
        if (arrayList.get(position).isHas_video()) {
            return TYPE_VIDEO;
        } else if (!arrayList.get(position).isHas_image()) {
            return TYPE_NO_PIC;
        } else if (arrayList.get(position).getLarge_image_list() != null && arrayList.get(position).getLarge_image_list().size() > 0) {
            return TYPE_LARGE_PIC;
        } else if (arrayList.get(position).getImage_list() != null && arrayList.get(position).getImage_list().size() > 1) {
            return TYPE_MULITY_PIC;
        }
        return TYPE_NORMAL;
    }

    @Override
    public int getViewTypeCount() {
        return 5;
    }

    @Override
    public View initView(final int position, View convertView, final ViewGroup parent) {
        //获取当前条目的类型
        int itemViewType = getItemViewType(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            switch (itemViewType) {
                case TYPE_NO_PIC:
                    convertView = CommonUtils.inflter(R.layout.type_no_pic_item);
                    viewHolder.tv_news_title = (TextView) convertView.findViewById(R.id.tv_news_title);
                    break;

                case TYPE_LARGE_PIC:
                    convertView = CommonUtils.inflter(R.layout.type_large_pic_item);
                    viewHolder.tv_news_title = (TextView) convertView.findViewById(R.id.tv_news_title);
                    viewHolder.iv_news_pic = (ImageView) convertView.findViewById(R.id.iv_news_pic);
                    break;

                case TYPE_MULITY_PIC:
                    convertView = CommonUtils.inflter(R.layout.type_multiple_pic_item);
                    viewHolder.tv_news_title = (TextView) convertView.findViewById(R.id.tv_news_title);
                    viewHolder.iv_news_pic1 = (ImageView) convertView.findViewById(R.id.iv_news_pic1);
                    viewHolder.iv_news_pic2 = (ImageView) convertView.findViewById(R.id.iv_news_pic2);
                    viewHolder.iv_news_pic3 = (ImageView) convertView.findViewById(R.id.iv_news_pic3);
                    break;

                case TYPE_NORMAL:
                    convertView = CommonUtils.inflter(R.layout.type_normal_item);
                    viewHolder.tv_news_title = (TextView) convertView.findViewById(R.id.tv_news_title);
                    viewHolder.iv_news_pic = (ImageView) convertView.findViewById(R.id.iv_news_pic);
                    break;

                case TYPE_VIDEO:
                    convertView = CommonUtils.inflter(R.layout.type_video_item);
                    viewHolder.tv_news_title = (TextView) convertView.findViewById(R.id.tv_news_title);
                    viewHolder.vv_news_movie = (VideoView) convertView.findViewById(R.id.vv_news_movie);
                    viewHolder.iv_news_pic = (ImageView) convertView.findViewById(R.id.iv_news_pic);
                    viewHolder.iv_news_movie_play = (ImageView) convertView.findViewById(R.id.iv_news_movie_play);
                    break;
            }
            viewHolder.tv_news_type = (TextView) convertView.findViewById(R.id.tv_news_type);
            viewHolder.tv_news_source = (TextView) convertView.findViewById(R.id.tv_news_source);
            viewHolder.tv_news_comment = (TextView) convertView.findViewById(R.id.tv_news_comment);
            viewHolder.iv_news_delete = (ImageView) convertView.findViewById(R.id.iv_news_delete);
            viewHolder.tv_news_time = (TextView) convertView.findViewById(R.id.tv_news_time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv_news_title.setText(arrayList.get(position).getTitle());
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
        if (CommonUtils.getBooleanFlag(arrayList.get(position).getTitle())){
            viewHolder.tv_news_title.setTextColor(Color.GRAY);
        }else {
            viewHolder.tv_news_title.setTextColor(Color.BLACK);
        }

=======
        viewHolder.tv_news_title.setTextColor(Color.BLACK);
        if (CommonUtils.getBooleanFlag(arrayList.get(position).getTitle())) {
            viewHolder.tv_news_title.setTextColor(Color.GRAY);
        } else {
            viewHolder.tv_news_title.setTextColor(Color.BLACK);
        }

        if (MyApplication.isNightMode) {
            viewHolder.tv_news_title.setTextColor(Color.WHITE);
        }

>>>>>>> NewsTop

        if (arrayList.get(position).getLabel() != null && arrayList.get(position).getLabel().equals("置顶")) {
            viewHolder.tv_news_type.setText("置顶");
        } else {
            viewHolder.tv_news_type.setText("热");
        }


        viewHolder.tv_news_source.setText(arrayList.get(position).getSource());
        viewHolder.tv_news_comment.setText("评论:" + arrayList.get(position).getComment_count() + "");
        viewHolder.tv_news_time.setText(arrayList.get(position).getPublish_time() / 1000 % 60 + "分钟前");

        switch (itemViewType) {
            case TYPE_NO_PIC:
                break;

            case TYPE_LARGE_PIC:
                //  viewHolder.iv_news_pic.setScaleType(ImageView.ScaleType.CENTER);
                ImageLoader.getInstance().displayImage(arrayList.get(position).getLarge_image_list().get(0).url, viewHolder.iv_news_pic, initOptions);
                break;

            case TYPE_MULITY_PIC:
                ImageLoader.getInstance().displayImage(arrayList.get(position).getImage_list().get(0).url, viewHolder.iv_news_pic1, initOptions);
                ImageLoader.getInstance().displayImage(arrayList.get(position).getImage_list().get(1).url, viewHolder.iv_news_pic2, initOptions);
                ImageLoader.getInstance().displayImage(arrayList.get(position).getImage_list().get(2).url, viewHolder.iv_news_pic3, initOptions);
                break;

            case TYPE_NORMAL:
                ImageLoader.getInstance().displayImage(arrayList.get(position).getMiddle_image().url, viewHolder.iv_news_pic, initOptions);
                break;

            case TYPE_VIDEO:
//                viewHolder.iv_news_pic.setScaleType(ImageView.ScaleType.CENTER);
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
           //     ImageLoader.getInstance().displayImage(arrayList.get(position).getLarge_image_list().get(0).url,viewHolder.iv_news_pic,initOptions);
=======
                ImageLoader.getInstance().displayImage(arrayList.get(position).getLarge_image_list().get(0).url, viewHolder.iv_news_pic, initOptions);
>>>>>>> NewsTop
                break;
        }

        //监听删除
        final View finalConvertView = convertView;
        viewHolder.iv_news_delete.setOnClickListener(new View.OnClickListener() {

            private PopupWindow popupWindow;

            @Override
            public void onClick(View v) {
                View popup_delete_item = CommonUtils.inflter(R.layout.popup_delete_item);
                Button popup_button = (Button) popup_delete_item.findViewById(R.id.bt_pop_ensure);
                popup_linearLayout = (LinearLayout) popup_delete_item.findViewById(R.id.popup_linearLayout);


                popupWindow = new PopupWindow(popup_delete_item, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                int[] location = new int[2];
                v.getLocationInWindow(location);
                //设置在外边可以点击
                popupWindow.setOutsideTouchable(true);
                popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                popupWindow.showAtLocation(v, Gravity.TOP, location[0], (int) (location[1] + finalConvertView.getHeight() * 0.1f));

<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
                if (arrayList.get(position).getFilter_words()!=null&&arrayList.get(position).getFilter_words().size()>0){
                    for (int i = 0; i < arrayList.get(position).getFilter_words().size(); i++) {
                        TextView textView = new TextView(context);
                        textView.setGravity(Gravity.CENTER);
                        textView.setOnClickListener(this);
                        textView.setText(arrayList.get(position).getFilter_words().get(i).getName());
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        params.setMargins(CommonUtils.dip2px(5),CommonUtils.dip2px(5),CommonUtils.dip2px(5),CommonUtils.dip2px(5));
                        popup_linearLayout.addView(textView,params);
=======
                if (arrayList.get(position).getFilter_words() != null && arrayList.get(position).getFilter_words().size() > 0) {
                    for (int i = 0; i < arrayList.get(position).getFilter_words().size(); i++) {
                        TextView textView = new TextView(context);
                        textView.setGravity(Gravity.CENTER);
                     //   textView.setOnClickListener(this);
                        textView.setText(arrayList.get(position).getFilter_words().get(i).getName());
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        params.setMargins(CommonUtils.dip2px(5), CommonUtils.dip2px(5), CommonUtils.dip2px(5), CommonUtils.dip2px(5));
                        popup_linearLayout.addView(textView, params);
>>>>>>> NewsTop
                    }
                }
                //确定按钮监听
                popup_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        arrayList.remove(position);
                        NewsCategoryAdapater.this.notifyDataSetChanged();
                        popupWindow.dismiss();
                    }
                });
            }
        });

        return convertView;
    }


    @Override
    public void onClick(View v) {
        for (int i = 0; i < popup_linearLayout.getChildCount(); i++) {
            TextView textView = (TextView) popup_linearLayout.getChildAt(i);
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
            if (textView == v){
                textView.setTextColor(Color.RED);
            }else {
=======
            if (textView == v) {
                textView.setTextColor(Color.RED);
            } else {
>>>>>>> NewsTop
                textView.setTextColor(Color.GRAY);
            }
        }
    }

    class ViewHolder {
        TextView tv_news_type, tv_news_source, tv_news_comment, tv_news_time, tv_news_title;
        ImageView iv_news_delete, iv_news_pic, iv_news_pic1, iv_news_pic2, iv_news_pic3, iv_news_movie_play;
        VideoView vv_news_movie;
    }
}
